package org.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.test.service.AdminService;
import org.test.vo.Admin;

import javax.servlet.http.HttpSession;

@Controller //注解处理器
@RequestMapping("/Admin")
public class AdminController {

    //注入服务层对象
    @Autowired
    AdminService adminService;

    //用户登录
    @RequestMapping("/adminLogin")
    public String adminLogin(Model model, Admin admin, HttpSession session){
        String username=admin.getUsername();
        String password =admin.getPassword();
        //判断是否是管理员
        if (username.equals("xhuang") && password.equals("123")){
            session.setAttribute("adminname", username); // 前台用户会话属性设置
            return "redirect:/Book/bookAll";
        }
        else{
            //查询用户表信息
            Admin adminLog=adminService.getAdminByUnameAndPwt(username,password);
            if (adminLog != null) {
                session.setAttribute("username", username); // 前台用户会话属性设置
                //转发至控制器
                return "redirect:/Book/UserbookAll";
            } else {
                model.addAttribute("message", "用户名和密码错误!");
                return "public/message";
            }
        }

    }

    //新用户注册
    @RequestMapping("/adminRegister")
    public String adminRegister(Model model,Admin admin,HttpSession session){
        String username=admin.getUsername();
        String password=admin.getPassword();
        //查询用户名是否被注册
        Admin adminRlog=adminService.getAdminByUname(username);
        if (adminRlog!=null){
            model.addAttribute("message","该用户名已被注册，请改个更靓的名字吧");
            return "public/message";
        }
        else {
            //注册
            int adminReg=adminService.addAdmin(username,password);
            if (adminReg !=0){
                model.addAttribute("message","恭喜你注册成功");
                return "public/message";
            }else{
                model.addAttribute("message","不知道为什么注册不成功，打电话问我吧：8888-8888");
                return "public/message";
            }

        }

    }

    //注销功能
    @RequestMapping("/outLogin")
    public String outLogin(HttpSession session){
        //通过session.invalidata()方法来注销当前的session
        session.invalidate();
        return "index";
    }

    //更改密码，用户名跳转
    @RequestMapping("/updateUserUrl")
    public String updateUserUrl(HttpSession session,Model model){
        String username= (String) session.getAttribute("username");
        //查询当前用户信息
        Admin admin=adminService.getAdminByUname(username);
        model.addAttribute("admin",admin);
        return "user/user-updateNew";
    }

    //更新用户信息
    @RequestMapping("/updateUser")
    public  String  update(HttpSession session,Model model,Admin admin){
        //更新信息
         int updateUser = adminService.updateUser(admin.getId(),admin.getUsername(),admin.getPassword());
        if (updateUser !=0){
            model.addAttribute("message","更改信息成功");
            return "public/message";
        }else{
            model.addAttribute("message","不知道更改不成功，打电话问我吧：8888-8888");
            return "public/message";
        }

    }

}
